package com.ruoyi.mall.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.vo.GoodsSpecificationsVo;
import com.ruoyi.mall.domain.bo.GoodsSpecificationsBo;
import com.ruoyi.mall.service.IGoodsSpecificationsService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品规格
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/specifications")
public class GoodsSpecificationsController extends BaseController {

    private final IGoodsSpecificationsService iGoodsSpecificationsService;

    /**
     * 查询商品规格列表
     */
    @SaCheckPermission("mall:specifications:list")
    @GetMapping("/list")
    public TableDataInfo<GoodsSpecificationsVo> list(GoodsSpecificationsBo bo, PageQuery pageQuery) {
        return iGoodsSpecificationsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品规格列表
     */
    @SaCheckPermission("mall:specifications:export")
    @Log(title = "商品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GoodsSpecificationsBo bo, HttpServletResponse response) {
        List<GoodsSpecificationsVo> list = iGoodsSpecificationsService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品规格", GoodsSpecificationsVo.class, response);
    }

    /**
     * 获取商品规格详细信息
     *
     * @param goodsSpecificationsId 主键
     */
    @SaCheckPermission("mall:specifications:query")
    @GetMapping("/{goodsSpecificationsId}")
    public R<GoodsSpecificationsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long goodsSpecificationsId) {
        return R.ok(iGoodsSpecificationsService.queryById(goodsSpecificationsId));
    }

    /**
     * 新增商品规格
     */
    @SaCheckPermission("mall:specifications:add")
    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GoodsSpecificationsBo bo) {
        return toAjax(iGoodsSpecificationsService.insertByBo(bo));
    }

    /**
     * 修改商品规格
     */
    @SaCheckPermission("mall:specifications:edit")
    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GoodsSpecificationsBo bo) {
        return toAjax(iGoodsSpecificationsService.updateByBo(bo));
    }

    /**
     * 删除商品规格
     *
     * @param goodsSpecificationsIds 主键串
     */
    @SaCheckPermission("mall:specifications:remove")
    @Log(title = "商品规格", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsSpecificationsIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] goodsSpecificationsIds) {
        return toAjax(iGoodsSpecificationsService.deleteWithValidByIds(Arrays.asList(goodsSpecificationsIds), true));
    }
}
